package com.example.xjtuse_pc.services;

import android.app.ActivityManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;

import java.util.List;

public class TaskClearService extends Service {
    private TaskKillBroadcast tk;
    private ActivityManager am;

    public TaskClearService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        am= (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        tk=new TaskKillBroadcast();
        registerReceiver(tk, new IntentFilter(Intent.ACTION_SCREEN_OFF));

       super.onCreate();
    }

    @Override
    public void onDestroy() {
        unregisterReceiver(tk);
        tk=null;
        super.onDestroy();
    }


    class TaskKillBroadcast extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("join","TaskKillBroadcast");
            List<ActivityManager.RunningAppProcessInfo> infos=  am.getRunningAppProcesses();
            for(ActivityManager.RunningAppProcessInfo info:infos){
                am.killBackgroundProcesses(info.processName);
            }

        }

    }

}
